import getIn from '../../../../../yali-sdk/utils/getIn';
import { SUCCESS } from '../../../../../yali-sdk/utils/success';
import { FAIL } from '../../../../../yali-sdk/utils/fail';
import addressListQuery from '../../../../../yali-sdk/business/address/addressListQuery';
import updateAddress from '../../../../../yali-sdk/business/address/updateAddress';
import { updateVariableParams, getListFooterTitle, showFail, showSuccess } from '../../../../config/common';
import { MEMBER_ADD_ADDRESS_PATH } from '../../../pathUrl';
import { addNotifier, removeNotifier, notify } from '../../../../../yali-sdk/utils/NotifyRegistryCenter';
const configPage = {
    data: {
        params: {
            page: 1,
            rows: 10,
        },
        tag: null, // 从产品详情或者下单页进来的标记
        addressList:{
          hasNext: undefined,
          isLoaded: undefined,
          list:[],
        }
    },
    onLoad() {
        const tag = getIn(this, ['options', 'tag']) || null; // 区分从什么地方进入地址列表，tag=1为订单页进入
        this.fetchAddressList();
        addNotifier('updateAddressList', () => this.onPullDownRefresh(), this);
        this.setData({ tag });
    },
    onUnload() {
        removeNotifier('updateAddressList');
    },
    fetchAddressList() {
        const token = getIn(getApp(), ['globalData', 'token']);
        const mParams = { ...this.data.params, token };
        this.setData({ "addressList.isLoaded": false });
        global.ActionDispatcher.execute(addressListQuery, mParams, (status, res) => {
          if (status == SUCCESS) {
            const data = res.data;
            let list = res.data.list;
            list = list && list.map((x) => {
              x = { item: x }
              if (x.item.is_default == 1) {
                x.isSelect = true;
              } else {
                x.isSelect = false;
              }
              return x
            });
            const newList = mParams.page == 1 ? list : this.data.addressList.list.concat(list);
            const addressList = {
              currentPage: this.data.params.page,
              hasNext: newList.length < res.data.total,
              isLoaded:true,
              list: newList,
              total: res.data.total,
            }
            const titleText = getListFooterTitle(addressList);
            this.setData({ addressList, titleText });
            wx.stopPullDownRefresh();
          } else if (status == FAIL) {
            this.setData({ "addressList.isLoaded": true });
            wx.stopPullDownRefresh();
          }
        });
    },
    /** 下拉刷新 */
    onPullDownRefresh() {
        const page = 1;
        const params = updateVariableParams(this.data.params, { page });
        this.data.params = params;
        this.fetchAddressList();
    },
    /** 上拉触底 */
    onReachBottom() {
        if (getIn(this.data, ['addressList', 'isLoaded']) && getIn(this.data, ['addressList', 'hasNext'])) {
            // 界面加载完, 且还有下一页的情况。
            const pages = getIn(this.data, ['params', 'page']) || 1;
            const params = updateVariableParams(this.data.params, { page: pages + 1 });
            this.data.params = params;
            this.fetchAddressList();
        }
    },
    /** 添加会员地址 */
    addAddress() {
        let queryString;
        if (getIn(this.options, ['tag']) == 1) {
            queryString = `tag=${1}`;
        }
        global.navigateHelper.navigateTo({
            pathname: MEMBER_ADD_ADDRESS_PATH,
            queryString,
        });
    },
    /** 设置默认地址和选择地址 */
    setDefault(e) {
        const id = getIn(e, ['currentTarget', 'dataset', 'id']);
        const is_default = Number(getIn(e, ['currentTarget', 'dataset', 'is_default']));
        if (this.data.tag == 1) {
          const { addressList } = this.data;
          const list = [...addressList.list];
          const newList = list && list.map((x) => {
            const item = x.item;
            if(item.id == id) {
              x.isSelect = true;
            } else {
              x.isSelect = false;
            }
            return x
          });
          this.setData({ "addressList.list": newList });
          notify && notify('updateProductAddress', id);
          global.navigateHelper.navigateBack();
        } else {
          const token = getIn(getApp(), ['globalData', 'token']);
          global.ActionDispatcher.execute(updateAddress, { token, id, is_default: 1 }, (status, result) => {
            if (status == SUCCESS) {
              wx.hideLoading();
              this.onPullDownRefresh();
              notify && notify('updateProductAddress');
              global.navigateHelper.navigateBack();
            } else if (status == FAIL) {
              wx.hideLoading();
              showFail((result && result.msg) || '设置默认地址失败');
            }
          });
        }
    },
    /** 编辑 */
    editAddress(e) {
        const id = getIn(e, ['currentTarget', 'dataset', 'id']);
        let queryString = `id=${id}`;
        if (getIn(this.options, ['tag']) == 1) {
            queryString = `id=${id}&tag=${1}`;
        }
        global.navigateHelper.navigateTo({
            pathname: MEMBER_ADD_ADDRESS_PATH,
            queryString,
        });
    },

};
Page(configPage);
